package DataStructure;

public class LinkedList {

	public static void main(String[] args) {
        LS lList = new LS();
        
        // add elements to LinkedList
        lList.add(1);
        lList.add(2);
        lList.add(3);
        lList.add(4);
        lList.add(5);
        lList.add(6,3);

 
        System.out.println("lList - print linkedlist: " + lList);
        System.out.println("lList.size() - print linkedlist size: " + lList.size());
        System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
        System.out.println("lList.get(3) - get 2rd element: " + lList.get(2));
        System.out.println("lList.remove(2) - remove 2nd element: " + lList.remove(2));
        System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
        
        System.out.println("lList.size() - print linkedlist size: " + lList.size());
        System.out.println("lList.get(3) - get 2rd element: " + lList.get(2));
        System.out.println("lList - print linkedlist: " + lList);
    }

	
	static class LS{
		private Node head;
		private int listCount;
		
		public LS()
		{
			head = new Node(0);
			listCount = 0;
		}
		
		public void add(int data)
		{
			Node temp = new Node(data);
			Node current = head;
			
			while(current.getNext() != null)
			{
				current = current.getNext();
			}
			current.setNext(temp);
			listCount++;
		}
		
		public void add(int data, int index)
		{
			Node temp = new Node(data);
			Node current = head;
			int i = 1;
			while(i < index && current.getNext() != null)
			{
				current = current.getNext();
				i++;
			}
			temp.setNext(current.getNext());
			current.setNext(temp);
			listCount++;
		}

		public int get(int index)
		{
			if(index <= 0)
			{
				return -1;
			}
			Node current = head;
			int i = 1;
			while(i < index && current.getNext() != null)
			{
				current = current.getNext();
				i++;
			}
			if(current.getNext() == null)
			{
				return -1;
			}
			return current.getNext().getData();
		}
		
		public void print()
		{
			Node current = head;
			String output = "[";
			for(int i = 1; current.getNext() != null; i++)
			{
				output+=current.getNext().getData() + ", ";
				current = current.getNext();
			}
			output = output.substring(0, output.length() -2 ) + "]";
			System.out.println(output);
		}
		public int size()
		{
			return listCount;
		}
		
		public boolean remove(int index)
		{
			if(index <= 0 || size() == 0)
			{
				return false;
			}
			
			Node current = head;
			for(int i = 1; i < index; i++)
			{
				if(current.getNext() == null)
					return false;
				current = current.getNext();
			}
			
			current.setNext(current.getNext().getNext());
			listCount--;
			return true;
		}
	}

	static class Node{
		Node next;
		int data;
		
		public Node(int val){
			next = null;
			data = val;
		}
		
		public Node(int val, Node nextNode)
		{
			next = nextNode;
			data = val;
		}
		
		public int getData()
		{
			return data;
		}
		
		public void setData(int val)
		{
			data = val;
		}
		
		public Node getNext()
		{
			return next;
		}
		
		public void setNext(Node nextNode)
		{
			next = nextNode;
		}
	}

}
